<?php
defined('BASEPATH') OR exit('No direct script access allowed');
/* 
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
class Check_ZJ extends CI_Controller{
    function __construct() {
        parent::__construct();
        //$this->load->library('Check');
    }
    function index($userid){
        $tablename = "ATDAILY";
        header("Access-Control-Allow-Origin:*");
        $one_date = date("Y-m-d",  time());
        $two_date = date("Y-m-d", strtotime("-1 day"));
        $three_date = date("Y-m-d", strtotime("-2 day"));
        $four_date = date("Y-m-d", strtotime("-3 day"));
        $five_date = date("Y-m-d", strtotime("-4 day"));
        $sat_date = date("Y-m-d", strtotime("-5 day"));
        $seven_date = date("Y-m-d", strtotime("-6 day"));
        $res_dataone = $this->get_checkdata($one_date, $userid, $tablename);
        $res_datatwo = $this->get_checkdata($two_date, $userid, $tablename);
        $res_datathree = $this->get_checkdata($three_date, $userid, $tablename);
        $res_datafour = $this->get_checkdata($four_date, $userid, $tablename);
        $res_datafive = $this->get_checkdata($five_date, $userid, $tablename);
        $res_datasat = $this->get_checkdata($sat_date, $userid, $tablename);
        $res_dataseven = $this->get_checkdata($seven_date, $userid, $tablename);
        
        $res = $this->Data_model->get_single_data(array("EMPNO" => $userid),$tablename);
        
        $weekarray=array("日","一","二","三","四","五","六");
        $result_data = array(
            "data" => array(
                "1" => array("checkData" => $res_dataone,
                                "href" => "collapseOne",
                                "date_show" => $one_date."(星期".$weekarray[date("w",time())].")"),
                "2" => array("checkData" => $res_datatwo,
                                "href" => "collapseTwo",
                                "date_show" => $two_date."(星期".$weekarray[date("w",strtotime("-1 day"))].")"),
                "3" => array("checkData" => $res_datathree,
                                "href" => "collapseThree",
                                "date_show" => $three_date."(星期".$weekarray[date("w",strtotime("-2 day"))].")"),
                "4" => array("checkData" => $res_datafour,
                                "href" => "collapseFour",
                                "date_show" => $four_date."(星期".$weekarray[date("w",strtotime("-3 day"))].")"),
                "5" => array("checkData" => $res_datafive,
                                "href" => "collapseFive",
                                "date_show" => $five_date."(星期".$weekarray[date("w",strtotime("-4 day"))].")"),
                "6" => array("checkData" => $res_datasat,
                                "href" => "collapseSat",
                                "date_show" => $sat_date."(星期".$weekarray[date("w",strtotime("-5 day"))].")"),
                "7" => array("checkData" => $res_dataseven,
                                "href" => "collapseSeven",
                                "date_show" => $seven_date."(星期".$weekarray[date("w",strtotime("-6 day"))].")"),
            ),
            'username' => $res['EMPNAME']
        );
        $this->load->view("Check_ZJ",$result_data);
    }
    
    /*
     * add by zhangxuelin 20170904 OA移动端跳转页面
     */
    public function indexByUserid(){
        $userid = $_REQUEST["userid"];
        $tablename = "ATDAILY";
        header("Access-Control-Allow-Origin:*");
        $one_date = date("Y-m-d",  time());
        $two_date = date("Y-m-d", strtotime("-1 day"));
        $three_date = date("Y-m-d", strtotime("-2 day"));
        $four_date = date("Y-m-d", strtotime("-3 day"));
        $five_date = date("Y-m-d", strtotime("-4 day"));
        $sat_date = date("Y-m-d", strtotime("-5 day"));
        $seven_date = date("Y-m-d", strtotime("-6 day"));
        $res_dataone = $this->get_checkdata($one_date, $userid, $tablename);
        $res_datatwo = $this->get_checkdata($two_date, $userid, $tablename);
        $res_datathree = $this->get_checkdata($three_date, $userid, $tablename);
        $res_datafour = $this->get_checkdata($four_date, $userid, $tablename);
        $res_datafive = $this->get_checkdata($five_date, $userid, $tablename);
        $res_datasat = $this->get_checkdata($sat_date, $userid, $tablename);
        $res_dataseven = $this->get_checkdata($seven_date, $userid, $tablename);
        
        $res = $this->Data_model->get_single_data(array("EMPNO" => $userid),$tablename);
        
        $weekarray=array("日","一","二","三","四","五","六");
        $result_data = array(
            "data" => array(
                "1" => array("checkData" => $res_dataone,
                                "href" => "collapseOne",
                                "date_show" => $one_date."(星期".$weekarray[date("w",time())].")"),
                "2" => array("checkData" => $res_datatwo,
                                "href" => "collapseTwo",
                                "date_show" => $two_date."(星期".$weekarray[date("w",strtotime("-1 day"))].")"),
                "3" => array("checkData" => $res_datathree,
                                "href" => "collapseThree",
                                "date_show" => $three_date."(星期".$weekarray[date("w",strtotime("-2 day"))].")"),
                "4" => array("checkData" => $res_datafour,
                                "href" => "collapseFour",
                                "date_show" => $four_date."(星期".$weekarray[date("w",strtotime("-3 day"))].")"),
                "5" => array("checkData" => $res_datafive,
                                "href" => "collapseFive",
                                "date_show" => $five_date."(星期".$weekarray[date("w",strtotime("-4 day"))].")"),
                "6" => array("checkData" => $res_datasat,
                                "href" => "collapseSat",
                                "date_show" => $sat_date."(星期".$weekarray[date("w",strtotime("-5 day"))].")"),
                "7" => array("checkData" => $res_dataseven,
                                "href" => "collapseSeven",
                                "date_show" => $seven_date."(星期".$weekarray[date("w",strtotime("-6 day"))].")"),
            ),
            'username' => $res['EMPNAME']
        );
        $this->load->view("Check_ZJ",$result_data);
    }

    public function Login(){
        if (isset($_GET['code'])){
            $code = $_GET['code'];
            //$res = $this->Data_model->get_single_data("name = '中骏森驰'","WEIXININFOZJ");
            $res = $this->Data_model->get_single_data(array("name" => "中骏森驰",
                                                            "type" => 'CheckZJ_4'),"WEIXININFOZJ");
            $this->get_userId($res['access_token'], $code,$res['agentid']);
        }else{
            echo "NO CODE";
        }
    }
    
    function get_userId($access_token,$code,$agentId){
        if($access_token){
            $url = "https://qyapi.weixin.qq.com/cgi-bin/user/getuserinfo?access_token=".$access_token."&code=".$code."&agentid=".$agentId;
            $json=self::http_request_json($url);//这个地方不能用file_get_contents
            $data=json_decode($json,true);
            if(isset($data['errcode']) & $data['errcode'] != 0){
                if($data['errcode'] == "40001" or $data['errcode'] == '42001' or $data['errcode'] == '40014'){
                    $this->get_access_token();
                    
                    /*
                    $url = "https://qyapi.weixin.qq.com/cgi-bin/user/getuserinfo?access_token=".$access_token."&code=".$code."&agentid=".$agentId;
					$json=self::http_request_json($url);//这个地方不能用file_get_contents
					$data=json_decode($json,true);
					$this->index($data['UserId']);
                     
                     * 
                     */
                    
                    $newurl = "http://weixin.gensenauto.com/WEIXIN/Check_ZJ/Login?code=".$code;  
                    echo "<script language='javascript' type='text/javascript'>";  
                    echo "window.location.href='".$newurl."'";  
                    echo "</script>"; 
                    
                }else{
                    echo "页面请求错误，请检查！";
                }
            }else{
                $this->index($data['UserId']);
            }
        }else{
            $this->get_access_token();
            $this->Login();
        }
    }
    function get_access_token(){
        $result = $this->Data_model->get_single_data(array("name" => "中骏森驰",
                                                            "type" => 'CheckZJ_4'),"WEIXININFOZJ");
        $corpid = $result['corpid'];
        $corpsecret = $result['corpsecret'];
        $url="https://qyapi.weixin.qq.com/cgi-bin/gettoken?&corpid=".$corpid."&corpsecret=".$corpsecret;
        $json=self::http_request_json($url);//这个地方不能用file_get_contents
        $data=json_decode($json,true);
        if(isset($data['access_token'])){
            $this->Data_model->update_data(array("name" => "中骏森驰",
                                                            "type" => 'CheckZJ_4'),array('access_token' => $data['access_token']),'WEIXININFOZJ');
        }else{
            return "获取access_token错误";
        }        
    }
    //因为url是https 所有请求不能用file_get_contents,用curl请求json 数据
    function http_request_json($url){  
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL,$url);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        $result = curl_exec($ch);
        curl_close($ch);
        return $result;  
    }
    function get_checkdata($date,$userid,$tablename){
        //$resData = $this->Data_model->get_single_data("CHECKDATE = '".$date."' and EMPNO = '".$userid."'",$tablename);
        $resData = $this->Data_model->get_data(array("CHECKDATE" => $date,
                                                        "EMPNO" => $userid),"","",-1,-1,$tablename);
        return $resData;
    }
}

